﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace SmsSend
{
    public class CommonFunction
    {
        #region 全角转半角
        //声明一些API函数
        [DllImport("imm32.dll")]
        public static extern IntPtr ImmGetContext(IntPtr hwnd);
        [DllImport("imm32.dll")]
        public static extern bool ImmGetOpenStatus(IntPtr himc);
        [DllImport("imm32.dll")]
        public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
        [DllImport("imm32.dll")]
        public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2);
        [DllImport("imm32.dll")]
        public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey);
        public const int IME_CMODE_FULLSHAPE = 0x8;
        public const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
        /// <summary>
        /// 全角转半角
        /// </summary>
        /// <param name="h"></param>
        public static void ChangeControlIme(IntPtr h)
        {
            IntPtr HIme = ImmGetContext(h);
            if (ImmGetOpenStatus(HIme)) //如果输入法处于打开状态
            {
                int iMode = 0;
                int iSentence = 0;
                bool bSuccess = ImmGetConversionStatus(HIme, ref iMode, ref iSentence); //检索输入法信息
                if (bSuccess)
                {
                    if ((iMode & IME_CMODE_FULLSHAPE) > 0) //如果是全角
                        ImmSimulateHotKey(h, IME_CHOTKEY_SHAPE_TOGGLE); //转换成半角
                }
            }
        }
        #endregion

        private void Control_Enter(object sender, EventArgs e)
        {
            Control ctl = (Control)sender;
            SmsSend.CommonFunction.ChangeControlIme(ctl.Handle);
        }
        public void SetControlIme(Form frm)
        {
            SetEnterEvent(frm);
        }

        public void SetControlIme(UserControl uc)
        {
            SetEnterEvent(uc);
        }
        public void SetEnterEvent(Control ctP)
        {
            foreach (Control ct in ctP.Controls)
            {
                if (Regex.IsMatch(ct.GetType().Name.ToString(), @"(ComboBoxEdit)|(TextEdit)|(DateTimePicker)|(ComboBox)|(SpinEdit)|(TextBox)|(ButtonEdit)|(GridControl)|(MemoEdit)"))
                {
                    ct.Enter += new EventHandler(Control_Enter);
                }
                SetEnterEvent(ct);
            }
        }

        /// <summary>
        /// by zhongxy 2011-09-15 验证手机号码格式
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static bool IsValidMobile(string mobile)
        {
            return Regex.IsMatch(mobile, @"^((\(\d{3}\))|(\d{3}\-))?13\d{9}$");
        }

        /// <summary>
        /// 对输入框的特殊字串进行过滤，防止SQL注入
        /// </summary>
        /// <param name="strFromText">要被过滤的字符串</param>
        /// <returns>过滤后的字符串</returns>
        public static string SqlInsertEncode(string strFromText)
        {
            if (!System.String.IsNullOrEmpty(strFromText) && strFromText != "")
            {
                strFromText = strFromText.Replace(";", "&#59;");
                strFromText = strFromText.Replace("!", "&#33;");
                strFromText = strFromText.Replace("@", "&#64;");
                strFromText = strFromText.Replace("$", "&#36;");
                strFromText = strFromText.Replace("*", "&#42;");
                strFromText = strFromText.Replace("(", "&#40;");
                strFromText = strFromText.Replace(")", "&#41;");
                strFromText = strFromText.Replace("-", "&#45;");
                strFromText = strFromText.Replace("+", "&#43;");
                strFromText = strFromText.Replace("=", "&#61;");
                strFromText = strFromText.Replace("|", "&#124;");
                strFromText = strFromText.Replace("\\", "&#92;");
                strFromText = strFromText.Replace("/", "&#47;");
                strFromText = strFromText.Replace(":", "&#58;");
                strFromText = strFromText.Replace("\"", "&#34;");
                strFromText = strFromText.Replace("'", "&#39;");
                strFromText = strFromText.Replace("<", "&#60;");
                strFromText = strFromText.Replace(" ", "&#32;");
                strFromText = strFromText.Replace(">", "&#62;");
                strFromText = strFromText.Replace(" ", "&#32;");
            }
            return strFromText;
        }

        /// <summary>
        /// 判断输入的文本框是否是数值型
        /// </summary>decimal
        /// <param name="txtvalues"></param>
        /// <returns></returns>
        public static bool Isdecimal(string txtvalues)
        {
            decimal val = 0;
            try
            {
                val = decimal.Parse(txtvalues);
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
